//
// Created by ta&to on 2022/10/28.
//
#include "http_net_config.h"
void init_rep_attr(struct response_head_attr_s* attr) {
    attr->Content_Length = -1;
    attr->status = -1;
    attr->version = NULL;
    attr->success = NULL;
    attr->Server = NULL;
    attr->Date = NULL;
    attr->Content_Type = NULL;
    attr->Last_Modified = NULL;
    attr->Transfer_Encoding = NULL;
    attr->Connection = NULL;
    attr->Vary = NULL;
    attr->ETag = NULL;
    attr->Content_Encoding = NULL;
    attr->Accept_Ranges = NULL;
}
void _port_rep_attr_do(struct responseContext *response, dts attr_str) {
    dts attr_key = dtsdup(attr_str);
    dts attr_val = dtsdup(attr_str);
    int idex = indexof(attr_key, ":");
    if(idex > -1) {
        dtsrange(attr_key, 0, idex - 1);
        dtsrange(attr_val, idex + 1, dtslen(attr_val));
        attr_key = dtstrim(attr_key, " ");
        attr_val = dtstrim(attr_val, " ");
        if (!dtscasecmpstr(attr_key, "Server")) {
            dts_auto_make(&response->attr->Server, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Date")) {
            dts_auto_make(&response->attr->Date, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Content-Type")) {
            dts_auto_make(&response->attr->Content_Type, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Last-Modified")) {
            dts_auto_make(&response->attr->Last_Modified, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Transfer-Encoding")) {
            dts_auto_make(&response->attr->Transfer_Encoding, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Connection")) {
            dts_auto_make(&response->attr->Connection, attr_val);
//        } else if (!dtscasecmpstr(attr_key, "Vary")) {
//            dts_auto_make(&response->attr->Vary, attr_val);
        } else if (!dtscasecmpstr(attr_key, "ETag")) {
            dts_auto_make(&response->attr->ETag, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Content-Encoding")) {
            dts_auto_make(&response->attr->Content_Encoding, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Content-Length")) {
            response->attr->Content_Length = atoi(attr_val);
            dtsfree(attr_val);
        } else if (!dtscasecmpstr(attr_key, "Accept-Ranges")) {
            dts_auto_make(&response->attr->Accept_Ranges, attr_val);
        } else {
            dtsfree(attr_val);
        }
        dtsfree(attr_key);
    }else {
        dtsfree(attr_key);
        dtsfree(attr_val);
    }
}
dts _port_rep_trim_head(dts head){
    dts head_ = dtsdup(head);
    int hidex = indexof(head, HEAD_TAIL);
    dtsrange(head_, 0, hidex + strlen(HEAD_TAIL) - 1);
    return head_;
}
void _file_rep_analysis_head(struct responseContext *response) {

}
static inline void __init_attr(rep_attr_t *__p_attr) {
    __p_attr->Content_Length = -1;
    __p_attr->status = -1;
    __p_attr->success = NULL;
}
void _port_rep_analysis_head(struct responseContext *response) {
    int idex;
    __init_attr(response->attr);
    dts head_ = _port_rep_trim_head(response->head);
    char version[BUF_SIZE] = { 0 };
    char status[10] = { 0 };
    char success[10] = { 0 };
    sscanf(response->head, "%[^ ] %[^ ] %[^\r\n]", version, status, success);
    response->attr->version = dtsnew(version);
    response->attr->status = atoi(status);
    response->attr->success = dtsnew(success);

    dts attr__ = dtsdup(head_);
    while((idex = indexof(attr__, CRLF)) > -1) {
        dtsrange(attr__, 0, idex - 1);
        dtsrange(head_, idex + sizeof CRLF  - 1, dtslen(head_));
        _port_rep_attr_do(response, attr__);
        dtsfree(attr__);
        attr__ = dtsdup(head_);
    }
    dtsfree(attr__);
    dtsfree(head_);
}

void _clear_rep_head(struct responseContext *response) {
    if(response->attr->version)
        dts_free_null(&response->attr->version);
    if(response->attr->success)
        dts_free_null(&response->attr->success);
    if(response->attr->Server)
        dts_free_null(&response->attr->Server);
    if(response->attr->Date)
        dts_free_null(&response->attr->Date);
    if(response->attr->Content_Type)
        dts_free_null(&response->attr->Content_Type);
    if(response->attr->Last_Modified)
        dts_free_null(&response->attr->Last_Modified);
    if(response->attr->Transfer_Encoding)
        dts_free_null(&response->attr->Transfer_Encoding);
    if(response->attr->Connection)
        dts_free_null(&response->attr->Connection);
    if(response->attr->Vary)
        dts_free_null(&response->attr->Vary);
    if(response->attr->ETag)
        dts_free_null(&response->attr->ETag);
    if(response->attr->Content_Encoding)
        dts_free_null(&response->attr->Content_Encoding);
    if(response->attr->Accept_Ranges)
        dts_free_null(&response->attr->Accept_Ranges);
}
